﻿#include "OperImportVTK.h"
#include <QFile>
#include <QFileInfo>
#include <QFileDialog>
#include <QAction>
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKComponents.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h"
#include "GUIFrame/MainWindow.h"
#include "GUIDialog/GUIToolDialog/GUIFileDirectoryDialog.h"

#include "GUIWidget/GraphArea.h"
#include "GUIFrame/CentralWidget.h"
#include "GUIWidget/GraphMdiArea.h"

#include "OperatorsInterface/TreeEventOperator.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include "FITK_Interface/FITKInterfaceStructuralPost/FITKAbstractStruPostVTK.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Interface/FITKInterfaceStructuralPost/FITKStruPost3DManager.h"
#include "FITK_Interface/FITKInterfaceStructuralPost/FITKDynamicStruPostVTK.h"
#include "FITK_Interface/FITKInterfaceStructuralPost/FITKStaticStruPostVTK.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossPostData.h"


namespace OperModel
{
    bool OperImportVTK::execGUI()
    {
        GUI::MainWindow* mainWin = dynamic_cast<GUI::MainWindow*>(FITKAPP->getGlobalData()->getMainWindow());
        if (mainWin == nullptr)return false;
        GUI::GUIFileDirectoryDialog* fileDialog = new GUI::GUIFileDirectoryDialog(this, mainWin);
        fileDialog->iniFileFilterType("Support Files(*.vtk;;*.dat;;*.plt;;*.cgns;;*.vtp)");

        //设置对话框路径
        AppFrame::FITKAppSettings* AppSetting = FITKAPP->getAppSettings();
        if (AppSetting) {
            QString workDir = AppSetting->getWorkingDir();
            fileDialog->initWin(workDir);
        }

        fileDialog->show();
        return false;
    }

    bool OperImportVTK::execProfession()
    {
        //文件存在
        QStringList allFileNames;
        bool ok = this->argValue<QStringList>("FileNames", allFileNames);
        if (!ok || allFileNames.isEmpty()) {
            return false;
        }
        //获取算例ID
        int caseID = -1, functionID = -1;
        //获取数据对象
        Radioss::FITKRadiossPostData* radiossPostData = Radioss::FITKRadiossPostData::GetDataFromAPPFrame();
        if (radiossPostData == nullptr) return false;

        //剔除已打开文件
        QStringList fileNames;
        for (auto filename : allFileNames) {
            if (!_filePathList.contains(filename))
                fileNames.append(filename);
        }
        if (fileNames.isEmpty())return true;

        // 根据文件后缀筛选数据类型。
        QFileInfo fileInfo(fileNames.at(0));
        QString type = fileInfo.suffix().toLower();
        Interface::StruPostFileType fileType = Interface::StruPostFileType::SPFTNone;
        if (type == "vtk")
            fileType = Interface::StruPostFileType::SPFTVTK;
        else if (type == "vtu")
            fileType = Interface::StruPostFileType::SPFTVTU;
        else if (type == "vtp")
            fileType = Interface::StruPostFileType::SPFTVTP;
        else
            return false;

        Interface::FITKAbstractStructuralPostVTK* dataObj = nullptr;
        if (fileNames.size() > 1) {
            //创建数据
            Interface::FITKDynamicStruPostVTK* postData = new Interface::FITKDynamicStruPostVTK(fileType);
            QFileInfo fileInfo(fileNames.at(0));
            postData->setFiles(fileNames);
            QString name = radiossPostData->getPost3DManager()->checkName(fileInfo.absolutePath());
            postData->setDataObjectName(name);
            dataObj = postData;
        }
        else {
            //创建数据
            Interface::FITKStaticStruPostVTK* postData = new Interface::FITKStaticStruPostVTK(fileType);
            postData->setFile(fileNames.at(0));
            //输出信息
            AppFrame::FITKMessageNormal(QString(tr("Reading vtk file %1 ...")).arg(fileNames.at(0)));
            QString name = radiossPostData->getPost3DManager()->checkName(fileNames.at(0));
            postData->setDataObjectName(name);
            dataObj = postData;
        }
        if (dataObj == nullptr)return false;

        _filePathList += fileNames;

        radiossPostData->getPost3DManager()->appendDataObj(dataObj);
        radiossPostData->setCurrentPostDataID(dataObj->getDataObjectID());

        EventOper::GraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPostProcess");
        if (graphOper)
        {
            graphOper->updateAll();
        }

        // 获取part树操作器
        EventOper::TreeEventOperator* treeOper = FITKOPERREPO->getOperatorT<EventOper::TreeEventOperator>("ResultTreeEvent");
        if (treeOper == nullptr) return false;
        treeOper->updateTree();

        // 设置第一帧数据
        Core::FITKActionOperator* oper = Core::FITKOperatorRepo::getInstance()->getOperatorT<Core::FITKActionOperator>("actionUpdateToolBarField");
        if (oper == nullptr) return false;
        QAction* action = new QAction();
        action->setObjectName("actionUpdateToolBarField");
        oper->setEmitter(action);
        oper->actionTriggered();

        delete action;
        return true;
    }

}



